import 'package:dance_todo/db/bean_event.dart';
import 'package:dance_todo/pages/controller_schedule.dart';
import 'package:dance_todo/res/res_colors.dart';
import 'package:dance_todo/utils/formatter_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class EventPage extends StatefulWidget {
  const EventPage({Key? key}) : super(key: key);

  @override
  State<EventPage> createState() => _EventPageState();
}

class _EventPageState extends State<EventPage> {
  final TextEditingController _nameInputCtrl = TextEditingController();
  final TextEditingController _countInputCtrl = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black87,
      child: SafeArea(
        child: Container(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  child: const Text(
                    '创建任务',
                    style: TextStyle(
                      color: ColorTitle,
                      fontSize: 70.0,
                    ),
                  ),
                ),
                Expanded(child: buildEventContainer()),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildEventContainer() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            child: Column(
              children: const [
                Text(
                  '名称',
                  style: TextStyle(
                    color: ColorActiveRed,
                    fontSize: 40.0,
                  ),
                ),
                Text(
                  '请输入任务的名称(最大长度6)',
                  style: TextStyle(
                    color: ColorHint,
                    fontSize: 16.0,
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 10.0),
          TextField(
            controller: _nameInputCtrl,
            cursorHeight: 20.0,
            cursorColor: ColorTitle,
            style: TextStyle(fontSize: 20.0, color: ColorTitle),
            keyboardType: TextInputType.text,
            maxLines: 1,
            maxLength: 6,
            decoration: const InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: ColorActiveRed, width: 1.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: ColorActiveRed, width: 1.0),
              ),
            ),
          ),
          const SizedBox(height: 10.0),
          Container(
            child: Column(
              children: const [
                Text(
                  '总数',
                  style: TextStyle(
                    color: ColorActiveRed,
                    fontSize: 40.0,
                  ),
                ),
                Text(
                  '个数范围0~300',
                  style: TextStyle(
                    color: ColorHint,
                    fontSize: 16.0,
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 10.0),
          TextField(
            controller: _countInputCtrl,
            cursorHeight: 20.0,
            cursorColor: ColorTitle,
            style: TextStyle(fontSize: 20.0, color: ColorTitle),
            keyboardType: TextInputType.numberWithOptions(
              signed: false,
              decimal: true,
            ),
            inputFormatters: [
              NumberInputLimit(
                inputScope: "1234567890",
                max: 300,
              ),
            ],
            maxLines: 1,
            decoration: InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: ColorActiveRed, width: 1.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: ColorActiveRed, width: 1.0),
              ),
            ),
          ),
          SizedBox(height: 40.0),
          GestureDetector(
            onTap: () {
              createEvent();
            },
            child: Container(
              padding: const EdgeInsets.symmetric(
                horizontal: 40.0,
                vertical: 20.0,
              ),
              decoration: BoxDecoration(
                color: Colors.grey.withAlpha(50),
                borderRadius: BorderRadius.circular(100.0),
              ),
              child: Text(
                '创建',
                style: TextStyle(
                  color: ColorTitle,
                  fontSize: 40.0,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///创建
  void createEvent() {
    String nameStr = _nameInputCtrl.text;
    String countStr = _countInputCtrl.text;
    int count = int.parse(countStr);
    var event=Event();
    event.name=nameStr;
    event.totalCount=count;
    event.doneCount=0;
    ScheduleCtrl.to.createEvent(event);
    ScheduleCtrl.to.queryEvents();
    Get.back();
  }
}
